package gameAppLayer.enums;

import gameAppLayer.GAERROR;
import gameAppLayer.GameError;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Defines mappings between game objects/states and textures
 */
public class TextureList {
	
	
//	private HashMap<String, HashMap<String, HashMap<String, String>>> data = new HashMap<String, HashMap<String, HashMap<String, String>>>();
	
	
//	private HashMap<Integer, ArrayList<String>> textureLists //integers reference line numbers in text file
//	private ArrayList<String> objectNames //names reference row headings in text file
//	private HashMap<String, Integer> //input String is objectNames object, Integer is its row number in text file
//	private HashMap<String, ArrayList<String>> //columnNames--reference column headings in text file, per table
//	//String is objectName (row), ArrayList<String> is columnNames
	
	
	private HashMap<String, ArrayList<String>> tables = new HashMap<String, ArrayList<String>>();
	private HashMap<String, ArrayList<String>> tableRows = new HashMap<String, ArrayList<String>>();
	private HashMap<String, ArrayList<String>> tableCols = new HashMap<String, ArrayList<String>>();
	
	public String getTextureName(String table, String rowName, String columnName) {
		ArrayList<String> requestedTable = tables.get(table);

		int row = tableRows.get(table).indexOf(rowName);
		int col = tableCols.get(table).indexOf(columnName);
		
		int index = col + tableCols.get(table).size()*row;
		
//		System.out.println("Row: " + row + " Column: "+ col + " Index: " + index);
//		System.out.println("Tex at this point: " + requestedTable.get(index));
		if ((row == -1) || (col == -1)) {
//			System.out.println("TextureList: Returning Error");
			return getTextureName("ErrorTable", "Error", "Error"); //Invalid row or col, cannot complete reference. Defaults to error texture.
		}
		else if (requestedTable.get(index).length()==0){ //texture cell empty, but we have a valid row/col combo
//			System.out.println("TextureList: Returning Default");
			return getTextureName(table, rowName, "Default"); //return default value for this row instead
		}
		else {			
//			System.out.println(requestedTable.get(index).length());
//			System.out.println("TextureList: Returning Correct Texture");
			return requestedTable.get(index);
		}
		
		
	}
	
	public int getNumTableRows(String table) {
		return tableRows.get(table).size();
	}
	
	public int getNumTableCols(String table) {
		return tableCols.get(table).size();
	}
	
	public ArrayList<String> getRowList(String table) {
		return tableRows.get(table);
	}
	
	public ArrayList<String> getColumnList(String table) {
		return tableCols.get(table);
	}
	
	public void addTable(String name, ArrayList<String[]> table) {
		ArrayList<String> rows = new ArrayList<String>();
		ArrayList<String> cols = new ArrayList<String>();
		ArrayList<String> fullTable = new ArrayList<String>();
		
		//String[][] newTable = new String[table.length-1][table[0].length-1];
		
		//extract row names
		for (int i = 1; i < table.size(); i++) {
			String[] currLine = table.get(i);
			if (!currLine[0].equalsIgnoreCase("End")) {
				rows.add(currLine[0]);
			}
			else {
				i = table.size(); //exit loop, we've reached the end--don't want to accidentally read another table's data
			}
		}
		tableRows.put(name, rows);
		
		//extract column names
		String[] firstRow = table.get(0);
		for (int i = 1; i < firstRow.length; i++) {
			cols.add(firstRow[i]);
		}
		tableCols.put(name, cols);
		
		//extract main data
		for (int i = 1; i < table.size(); i++) {
			String[] currLine = table.get(i);
			for (int j = 1; j < firstRow.length; j++) {
				if (j >= currLine.length) {
					fullTable.add("");
				}
				else {
					fullTable.add(currLine[j]);
				}								
			}
		}		
		tables.put(name, fullTable);		
	}
	
	
	
	//Instances when table name isn't known
//	public String getTextureName(String rowName, String columnName) {
//		
//	}
	
	
	
	

}
